package com.example.moshudriver.fragment;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.BeeFramework.Utils.Utils;
import com.BeeFramework.model.BusinessResponse;
import com.sgr.dykdriver.R;
import com.example.moshudriver.activity.CouponActivity;
import com.example.moshudriver.activity.MyIncomeActivity;
import com.example.moshudriver.activity.MyVipActivity;
import com.example.moshudriver.activity.RechargeActivity;
import com.example.moshudriver.activity.SlidingActivity;
import com.example.moshudriver.activity.WithdrawActivity;
import com.example.moshudriver.model.UserBalanceModel;
import com.example.moshudriver.protocol.ApiInterface;
import com.example.moshudriver.tools.MessageConstant;
import com.example.moshudriver.tools.O2OMobileAppConst;
import com.example.moshudriver.tools.SESSION;
import com.external.androidquery.callback.AjaxStatus;
import com.external.eventbus.EventBus;
import com.external.maxwin.view.IXListViewListener;
import com.nostra13.universalimageloader.core.ImageLoader;

public class MyBalanceFragment extends Fragment implements IXListViewListener,
		BusinessResponse, OnClickListener {
	private ImageView iv_home_menu;
	private int mUserId;
	private UserBalanceModel mUserBalance;
	protected ImageLoader mImageLoader = ImageLoader.getInstance();
	private TextView my_balance, my_coupon, my_withdraw, top_view_title;
	private RelativeLayout rl_balance, rl_coupon, rl_my_income, rl_recharge,
			rl_withdraw;
	private RelativeLayout rl_get_myincome, rl_my_vip;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		SharedPreferences sp = getActivity().getSharedPreferences(
				O2OMobileAppConst.USERINFO, 0);
		mUserId = sp.getInt("uid", 0);
		View view = inflater.inflate(R.layout.my_balance, null);
		view.setOnClickListener(null);
		initView(view);

		mUserBalance = new UserBalanceModel(getActivity());
		mUserBalance.addResponseListener(this);
		mUserBalance.getProfile(mUserId);
		if (mUserId == SESSION.getInstance().uid) {
			mUserBalance.get();
		}
		if (!EventBus.getDefault().isregister(this)) {
			EventBus.getDefault().register(this);
		}
		return view;
	}

	private void initView(View view) {

		top_view_title = (TextView) view.findViewById(R.id.top_view_title);
		top_view_title.setText(R.string.my_purse);
		// 我的会员
		rl_my_vip = (RelativeLayout) view.findViewById(R.id.rl_my_vip);
		// 余额
		rl_balance = (RelativeLayout) view.findViewById(R.id.rl_balance);
		// 优惠券
		rl_coupon = (RelativeLayout) view.findViewById(R.id.rl_coupon);
		// 交易明细
		rl_my_income = (RelativeLayout) view.findViewById(R.id.rl_my_income);
		// 充值
		rl_recharge = (RelativeLayout) view.findViewById(R.id.rl_recharge);
		// 提现
		rl_withdraw = (RelativeLayout) view.findViewById(R.id.rl_withdraw);
		// 我的收益
		rl_get_myincome = (RelativeLayout) view.findViewById(R.id.rl_my_income);
		rl_balance.setOnClickListener(this);
		rl_coupon.setOnClickListener(this);
		rl_my_income.setOnClickListener(this);
		rl_recharge.setOnClickListener(this);
		rl_withdraw.setOnClickListener(this);
		rl_get_myincome.setOnClickListener(this);
		rl_my_vip.setOnClickListener(this);

		iv_home_menu = (ImageView) view.findViewById(R.id.top_view_back);
		// 余额
		my_balance = (TextView) view.findViewById(R.id.tv_get_balance);
		// 代金劵张数
		my_coupon = (TextView) view.findViewById(R.id.tv_get_coupon);
		// 可提现金额
		my_withdraw = (TextView) view.findViewById(R.id.tv_get_withdraw);

	}

	@Override
	public void onResume() {
		if (mUserId == SESSION.getInstance().uid) {
			mUserBalance.get();
		}
		super.onResume();
	}

	@Override
	public void onDestroyView() {
		if (EventBus.getDefault().isregister(this)) {
			EventBus.getDefault().unregister(this);
		}
		super.onDestroyView();
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		iv_home_menu.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				((SlidingActivity) getActivity()).showLeft();
			}
		});
	}

	public void onEvent(Object event) {
		Message message = (Message) event;
		if (message.what == MessageConstant.SHOW_MY_BALANCE) {
			mUserBalance.get();
			// mUserModel.getInviteCode();
		}
	}

	@Override
	public void onRefresh(int id) {
	}

	@Override
	public void onLoadMore(int id) {
	}

	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException {
		if (url.endsWith(ApiInterface.USER_BALANCE)) {
			Log.i("余额", "yue");
			my_balance.setVisibility(View.VISIBLE);
			my_balance.setText(Utils.formatBalance(mUserBalance.publicBalance)
					+ getString(R.string.yuan));
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rl_balance:

			break;
		case R.id.rl_coupon:// 优惠券
			Intent intent3 = new Intent();
			intent3.setClass(getActivity(), CouponActivity.class);
			startActivity(intent3);
			// Toast.makeText(getActivity(), "优惠券", Toast.LENGTH_SHORT).show();
			break;
		case R.id.rl_my_income:// 我的收益
			Intent intent4 = new Intent();
			intent4.setClass(getActivity(), MyIncomeActivity.class);
			startActivity(intent4);
			break;
		case R.id.rl_recharge:// 充值
			Intent intent2 = new Intent();
			intent2.setClass(getActivity(), RechargeActivity.class);
			startActivity(intent2);
			break;
		case R.id.rl_withdraw:// 提现

			Intent intent1 = new Intent();
			intent1.setClass(getActivity(), WithdrawActivity.class);
			startActivity(intent1);

			break;
		case R.id.rl_my_vip:// 我的会员

			Intent intent5 = new Intent();
			intent5.setClass(getActivity(), MyVipActivity.class);
			startActivity(intent5);

			break;
		// case R.id.rl_get_myincome:
		// Toast.makeText(getActivity(), "我的收益", Toast.LENGTH_SHORT).show();
		// break;
		default:
			break;
		}

	}

}
